package api

import (
	"github.com/labstack/echo/v4"
	"longmen/server/config/global"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/grpc_client"
	"longmen/server/pkg/protobuf"
)

// @Summary 主播申请
// @Description 主播申请
// @Accept  json
// @Produce  json
// @Tags AuthAnchor
// @Param   sendBody body api_models.IdentityAuthReq true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/auth/identityAuth [POST]
func (base *Controller) IdentityAuth(c echo.Context) error {
	r := &api_models.IdentityAuthReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(r); err != nil {
		global.Log.Errorf("6IdentityAuth9", err)
		return util.BadParam(c, err)
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	err = util.CheckMobile(r.Mobile)
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	err = base.Service.IdentityAuth(r, user)
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, nil, "")
}

// @Summary SendBindCode
// @Description SendBindCode
// @Accept  json
// @Produce  json
// @Tags AuthAnchor
// @Param   sendBody body api_models.SendBindCodeReq true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/auth/sendBindCode [POST]
func (base *Controller) SendBindCode(c echo.Context) error {
	r := &api_models.SendBindCodeReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	ip := c.RealIP()
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcAccountServer.SendSmsCode(ctx, &protobuf.SendSmsCodeReq{
		Ip:     ip,
		Mobile: r.Mobile,
	})
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 获取主播申请时的资料
// @Description 获取主播申请时的资料
// @Accept  json
// @Produce  json
// @Tags AuthAnchor
// @Success 200 {object} api_models.AuthResp "result"
// @Router /api/auth/getAuthInfo [POST]
func (base *Controller) GetAuthInfo(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	rsp, err := base.Service.GetAuthInfo(user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, rsp, "")
}
